#include "dsc3d.h"

/*	 
	This function is used to retrieve single word from a file. Terminating
	characters can be specified.

	Courtesy to George Passalis
*/
void get_word(FILE *fp, char *str, int dummy = 0) {
	int counter = 0, temp;
	char c;
	bool START = false;
	
	while ((temp = fgetc(fp)) != EOF) {
		c = (char)temp;
		if ((c == ' ') || (c == ',') || (c == '\t') || (c == '\0') || (c == '\n') || (c == '=') || (c == '\r')) {
			if (START) {
				str[counter]='\0';
				return;
			}
		}
		else {
			if (!START) START = true;
			str[counter] = c;
			counter++;
		}
	}
}


DSC3D::DSC3D() {}

DSC3D::~DSC3D() {}

int DSC3D::load_dsc3d_file(char *filename) {

	FILE *fp;
	char str[512];
	int counter = 0;
	
	if ((fp = fopen(filename, "r")) == NULL) {
		std::cerr << "Failed to open " << filename << " for reading" << std::endl;
		exit(-1);
	}
		
	get_word(fp, str);
	n = atof(str);

	get_word(fp, str);
	m = atof(str);

	get_word(fp, str);
	p = atof(str);

	get_word(fp, str);
	strcpy_s(orig_file_name, str);

	get_word(fp, str);
	strcpy_s(time_scanned, str);
	get_word(fp, str);
	while (strcmp(str, "Created") != 0) {
		strcat_s(time_scanned, 128, " ");
		strcat_s(time_scanned, 128, str);
		get_word(fp, str);
	}
	
	strcpy_s(time_created, str);
	get_word(fp, str);
	while (strcmp(str, "BinaryFlag") != 0) {
		if (strcmp(str, "0") == 0 || strcmp(str, "1") == 0) break;
		strcat_s(time_created, 128, " ");
		strcat_s(time_created, 128, str);
		get_word(fp, str);
	}
		
	binaryflag = atoi(str);
	/* dummy string */
	get_word(fp, str);
	
	vertices = new float* [p];
	texcoords = new float* [p];
	color = new float* [p];
	while (!feof(fp)) {
		if (counter == p) break;

		*(vertices + counter) = new float [3];
		for (int i = 0; i < 3; i++) {
			get_word(fp, str);
			vertices[counter][i] = atof(str);
		}
		
		*(texcoords + counter) = new float [2];
		for (int i = 0; i < 2; i++) {
			get_word(fp, str);
			texcoords[counter][i] = atof(str);
		}
		
		*(color + counter) = new float [3];
		for (int i = 0; i < 3; i++) {
			get_word(fp, str);
			color[counter][i] = atof(str);
		}
		
		counter++;
	}
	
	std::cerr << "Successfully loaded file: " << filename << std::endl;
	return 0;
}

int DSC3D::verify_loaded_data(int pts2show) {

	if (pts2show <= 0 || pts2show > p)
		pts2show = p;

	printf("Current DSC3D Data Info\n");
	printf("Dimension: %d x %d\n", n, m);
	printf("Vertex count: %d\n", p);
	printf("Originial filename: %s\n", orig_file_name);
	printf("Time scanned: %s\n", time_scanned);
	printf("Time created: %s\n", time_created);
	printf("Binary? %s\n", binaryflag == 0 ? "N" : "Y");
	for (int i = 0; i < pts2show; i++) {
		printf("%d: %f %f %f | %f %f | %f %f\n", i + 1,
				vertices[i][0], vertices[i][1], vertices[i][2],
				texcoords[i][0], texcoords[i][1],
				color[i][0], color[i][1], color[i][2]);
	}

	return 0;
}

int DSC3D::getVertexCount() {return p;}

float** DSC3D::getVertices() {return vertices;}

float** DSC3D::getTexcoords() {return texcoords;}